local hx__tianyi = fk.CreateSkill {
  name = "hx__tianyi",
}

hx__tianyi:addEffect("active", {
  name = "hx__tianyi",
  anim_type = "offensive",
  prompt = "#hx__tianyi",
  max_card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(hx__tianyi.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player and player:canPindian(to_select, true)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:drawCards(player, 1, hx__tianyi.name)
    local pindian = player:pindian({target}, hx__tianyi.name)
    if pindian.results[target].winner == player then
      local slash = Fk:cloneCard('slash')
      slash.skillName = hx__tianyi.name
      room:useCard({
        from = player,
        tos = {target},
        card = slash,
        extraUse = true,
        disresponsiveList = table.filter(room.alive_players, Util.IdMapper),
      })
    else
      room:addPlayerMark(player, "@hx__tianyi_chusha", 1)
      player.room:obtainCard(player, pindian.fromCard)
    end
  end
})

hx__tianyi:addEffect(fk.PindianCardsDisplayed, {
  anim_type = "control",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return data.from == player and data.reason == "hx__tianyi"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:sendLog{
      type = "#hx__tianyi_pindian",
      from = player.id,
      arg = player:getAttackRange(),
    }
    data.fromCard.number = math.min(data.fromCard.number + player:getAttackRange(), 13)
  end,
})

hx__tianyi:addEffect(fk.TurnEnd, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
     return player:hasSkill(hx__tianyi.name) and player:getMark("@hx__tianyi_chusha") > 0
  end,
  on_use = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "@hx__tianyi_chusha", 0)
  end,
})

hx__tianyi:addEffect("targetmod", {
  anim_type = "offensive",
  is_delay_effect = true,
  residue_func = function(self, player, skill, scope)
    if player:hasSkill(hx__tianyi.name) and skill.trueName == "slash_skill"
      and scope == Player.HistoryPhase and player:getMark("@hx__tianyi_chusha") > 0 then
      return 1
    end
  end,
})

return hx__tianyi